import { useSessionStorage, useStorage } from "@vueuse/core";
import {
  defaultThemeConfig,
  defaultFtpStorage,
  defaultWebStorage,
  defaultTagViewStorage,
  defaultServerStorage,
  defaultAudioHistoryStorage,
  defaultRunningStorage,
} from "./defaults";
import type { RemovableRef } from "@vueuse/core";

/**
 * 根据环境设置 storage key
 * @param key
 */
export function getStorageKey(key: string): string {
  if (key === "token") return key;
  return `deploy:${key}`;
}

// 主题配置
export const themeConfig: RemovableRef<ThemeConfigType> = useStorage(
  getStorageKey("theme_config"),
  defaultThemeConfig()
);

// FTPS
export const ftpStorage: RemovableRef<DeployFtpType[]> = useStorage(
  getStorageKey("ftpStorage"),
  defaultFtpStorage()
);

// FTPS Expand
export const ftpExpandStorage: RemovableRef<string[]> = useStorage(
  getStorageKey("ftpExpandStorage"),
  []
);

// WEB
export const webStorage: RemovableRef<WebType[]> = useStorage(
  getStorageKey("webStorage"),
  defaultWebStorage()
);

// WEB Expand
export const webExpandStorage: RemovableRef<string[]> = useStorage(
  getStorageKey("webExpandStorage"),
  []
);

// SERVER
export const serverStorage: RemovableRef<WebType[]> = useStorage(
  getStorageKey("serverStorage"),
  defaultServerStorage()
);

// SERVER Expand
export const serverExpandStorage: RemovableRef<string[]> = useStorage(
  getStorageKey("serverExpandStorage"),
  []
);

// Running
export const runStorage: RemovableRef<RunType[]> = useStorage(
  getStorageKey("runStorage"),
  defaultRunningStorage()
);

// AudioHistory
export const audioHistoryStorage: RemovableRef<AudioType[]> = useStorage(
  getStorageKey("audioHistoryStorage"),
  defaultAudioHistoryStorage()
);

// TagView
export const tagViewStorage: RemovableRef<WebRouteRaw[]> = useSessionStorage(
  getStorageKey("tagViewStorage"),
  defaultTagViewStorage()
);

// 全局TTS
export const synthOption: RemovableRef<SynthOption> = useSessionStorage(
  getStorageKey("synthOption"),
  {
    pending: false,
    speaking: false,
    paused: true,
  }
);

// 版本号
export const version: RemovableRef<string | undefined> = useStorage(
  getStorageKey("version"),
  undefined
);

// 用户token
export const token: RemovableRef<string | undefined> = useSessionStorage(
  getStorageKey("token"),
  undefined
);
